<script setup>
import { ref, defineEmits, defineProps } from 'vue'
import { ElMessage } from 'element-plus'
import { addApi, updateApi } from '@/api/logistics/logisticsPrice'

const props = defineProps({
  companyList: {
    type: Array,
    default: () => []
  }
})

const ruleFormRef = ref(null)
const rules = ref({
  logisticsCompanyId: [
    { required: true, message: '请选择', trigger: 'blur' },
    // { min: 3, max: 10, message: '长度应该在3-10', trigger: 'blur' },
  ],
  category: [
    { required: true, message: '请选择', trigger: 'blur' },
  ],
  cubePrice: [
    { required: true, message: '请输入，不能为空', trigger: 'blur' },
  ],
  kilogramPrice: [
    { required: true, message: '请输入，不能为空', trigger: 'blur' },
  ],
  transKilometerPrice: [
    { required: true, message: '请输入，不能为空', trigger: 'blur' },
  ]
})
const emit = defineEmits(['refresh'])

const dialogVisible = ref(false)
const dialogFormData = ref({
  id: null,
  cubePrice: 0,
  kilogramPrice: 0,
  transKilometerPrice: 0,
  category: null,
  logisticsCompanyId: null,
  saasId: null,
  isDel: 1
})

const resetForm = () => {
  dialogVisible.value = false
  dialogFormData.value = {
    id: null,
    cubePrice: 0,
    kilogramPrice: 0,
    transKilometerPrice: 0,
    category: null,
    logisticsCompanyId: null,
    saasId: null,
    isDel: 1
  }
  ruleFormRef.value.resetFields()
}

const confirm = async () => {
  // if (!dialogFormData.value.logisticsCompanyId) {
  //   ElMessage.error('请选择物流公司')
  //   return
  // }
  let res = null;
  if (!ruleFormRef) return
  await ruleFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      if (dialogFormData.value.id) {
        res = await updateApi(dialogFormData.value)
      } else {
        res = await addApi(dialogFormData.value)
      }
      if (res.code) {
        ElMessage.success('操作成功!')
        resetForm()
        instance.emit('search')
      } else {
        ElMessage.error('操作失败！')
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
defineExpose({
  dialogFormData,
  dialogVisible
})
</script>

<template>
  <el-dialog v-model="dialogVisible" title="物流价格信息" width="500px">
    <el-form :model="dialogFormData" label-width="120px" ref="ruleFormRef" :rules="rules">
      <el-form-item label="物流公司" prop="logisticsCompanyId">
        <el-select v-model="dialogFormData.logisticsCompanyId" placeholder="请选择物流公司">
          <el-option v-for="company in companyList" :key="company.id" :label="company.companyName"
            :value="company.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="类型" prop="category">
        <el-select v-model="dialogFormData.category" placeholder="请选择类型">
          <el-option label="同城" :value="1" />
          <el-option label="同省" :value="2" />
          <el-option label="跨省" :value="3" />
          <el-option label="经济区互寄" :value="4" />
        </el-select>
      </el-form-item>

      <el-form-item label="每立方费用(分)" prop="cubePrice">
        <el-input-number v-model="dialogFormData.cubePrice" :min="0" />
      </el-form-item>

      <el-form-item label="每千克费用(分)" prop="kilogramPrice">
        <el-input-number v-model="dialogFormData.kilogramPrice" :min="0" />
      </el-form-item>

      <el-form-item label="每千米费用(分)" prop="transKilometerPrice">
        <el-input-number v-model="dialogFormData.transKilometerPrice" :min="0" />
      </el-form-item>

      <el-form-item label="是否有效">
        <el-switch v-model="dialogFormData.isDel" :active-value="1" :inactive-value="2" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>